package com.biz.controller.basic;

import com.alibaba.fastjson.JSON;
import com.biz.model.Hmodel.basic.Tagent;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.sys.Dynamic;
import com.biz.model.Pmodel.sys.Puser;
import com.biz.service.basic.DynamicServiceI;
import com.framework.controller.BaseController;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.SqlFactory;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/dynamic")
public class DynamicController extends BaseController {

    @Autowired
    private DynamicServiceI dynamicService;

    @InitBinder("dynamic")
    public void initBinderFormBean1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("dynamic.");
    }

    @RequestMapping("toDynamic")
    public ModelAndView toMenu(ModelAndView mv){
        mv.setViewName("basic/dynamic/dynamic");
        return mv;
    }

    @RequestMapping("toEdit")
    public ModelAndView toEdit(HttpServletResponse response,String id)throws Exception {
        mv.clear();
        mv.addObject("id",id);
        Dynamic dynamic=dynamicService.findById(id);
        mv.addObject("row",dynamic);
        mv.setViewName("basic/dynamic/dynamic_detail");
        return mv;

    }

    @RequestMapping("showDynamic")
    public void showAgent(HttpServletResponse response, HttpServletRequest request){
        //必要的分页参数
        SqlFactory factory=new SqlFactory();
        Params sqlParams=factory.getParams();
        sqlParams.setOrder(request.getParameter("order"));
        sqlParams.setPage(Integer.parseInt(request.getParameter("offset")));
        sqlParams.setRows(Integer.parseInt(request.getParameter("limit")));
        sqlParams.setSort(request.getParameter("sort"));
        //  System.out.println(request.getParameter("customer"));
        sqlParams.setSearchtext(request.getParameter("customer")==null?"":request.getParameter("customer").toString());
        Paging paging=dynamicService.findDynamicGrid(sqlParams);
        System.out.println(JSON.toJSONString(paging));
        writeJson(paging,response);
    }

    /**
     * 保存
     * @param response
     * @param request
     */
    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response, HttpServletRequest request,Dynamic dynamic)throws Exception{
        String msg ="";
        //判断id是否为空不为空则更新
        if(!StringUtil.isNullOrEmpty(dynamic.getId())){
            dynamic.setCreatTime(new Date());
            dynamicService.updateDynamic(dynamic);
            msg = "success";
        }
        else{
            //id
            String id = UuidUtil.get32UUID();
            dynamic.setId(id);
            //创建时间
            dynamic.setCreatTime(new Date());
            //创建人
            User user = (User)getShiroAttribute("user");
            String releasePerson = user.getId();
            dynamic.setReleasePerson(releasePerson);
            //状态
            Short isdel = 0;
            dynamic.setIsdel(isdel);
            //审核
            Short state = 0;
            dynamic.setState(state);
            dynamicService.saveDynamic(dynamic);
            msg = "success";
        }
        writeJson(msg,response);
    }

    //编辑
    @RequestMapping("updateDynamic")
    public void updateDynamic(HttpServletResponse response, HttpServletRequest request,Dynamic dynamic)throws Exception{
        //
    }

    //删除
    @RequestMapping("delDynamicById")
    public void delDynamicById(HttpServletResponse response, HttpServletRequest request,String ids) throws Exception {
        Map<String,Object> map=new HashMap<>();
        try{
            dynamicService.delDynamicById(ids);
            map.put("success",true);
        }
        catch (Exception e)
        {
            map.put("success",false);
        }
        writeJson(map,response);
    }

}


